﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SmartWaterMointerCenter.UControls
{
    public partial class ULine : UserControl
    {
        public ULine()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.AutoSize = false;
            Size = new Size(100, 10);
        }
        Rectangle rect;
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            rect = this.ClientRectangle;
        }

        private bool isHorizontal = true;
        [DefaultValue(typeof(bool), "True"), Description("线的方向")]
        public bool IsHorizontal
        {
            get { return isHorizontal; }
            set
            {
                bool oldIsHorizontal = isHorizontal;
                isHorizontal = value;
                if (oldIsHorizontal != isHorizontal)
                {
                    Size = new Size(Size.Height, Size.Width);//宽高切换
                }
                Invalidate();
            }
        }

        private Color lineColor = Color.Gray;
        [DefaultValue(typeof(Color), "Gray"), Description("线的颜色")]
        public Color LineColor
        {
            get { return lineColor; }
            set
            {
                lineColor = value;
                Invalidate();
            }
        }

        private int lineWidth = 1;
        [DefaultValue(typeof(int), "1"), Description("线的粗细")]
        public int LineWidth
        {
            get { return lineWidth; }
            set
            {
                lineWidth = value;
                Invalidate();
            }
        }

        //控件重绘
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            Pen pen = new Pen(lineColor, lineWidth);//画直线或曲线的对象

            //画线：起点、终点坐标
            Point point1, point2;
            if (isHorizontal)
            {
                point1 = new Point(1, (Height - lineWidth) / 2);
                point2 = new Point(Width - 2, (Height - lineWidth) / 2);
            }
            else
            {
                point1 = new Point((Width - lineWidth) / 2, 1);
                point2 = new Point((Width - lineWidth) / 2, Height - 2);
            }
            g.DrawLine(pen, point1, point2);
        }
    }
}
